package com.wkck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wkck.VO.CareerDetailsVo;
import com.wkck.VO.CareerListDetails;
import com.wkck.VO.PageResult;
import com.wkck.annotaion.ClearAllCache;
import com.wkck.domain.Company;
import com.wkck.domain.Job;
import com.wkck.domain.Job_join;
import com.wkck.dto.CareerLikeQueryArgumentsDto;
import com.wkck.dto.SaveAndUpdateCareerDetailsDto;
import com.wkck.exception.BaseException;
import com.wkck.exception.CareerIDIsNotFoundException;
import com.wkck.mapper.CareerMapper;
import com.wkck.mapper.CompanyMapper;
import com.wkck.mapper.JobJoinMapper;
import com.wkck.mapper.JobMapper;
import com.wkck.service.CareerService;
import com.wkck.service.JobJoinService;
import com.wkck.service.JobService;
import com.wkck.utils.CareerInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * @Author: CK
 * @Date: 2023/12/17/20:38
 * @Description:
 */
@Service
@Slf4j
public class CareerServiceImpl implements CareerService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobJoinMapper jobJoinMapper;

    @Autowired
    private CareerMapper careerMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Resource
    private JobService jobService;

    private final String[] locations = {
            "/img/defaultImage/111.jpg",
            "/img/defaultImage/222.jpg",
            "/img/defaultImage/333.jpg",
            "/img/defaultImage/444.jpg",
            "/img/defaultImage/555.jpg",
            "/img/defaultImage/666.jpg",
            "/img/defaultImage/777.jpg",
            "/img/defaultImage/888.jpg",
            "/img/defaultImage/999.jpg"
    };

    /**
     * @Author: CK
     * @Date: 2023/12/17
     * @Param: []
     * @return: 岗位列表
     * @Description: 岗位列表
     */
    @Override
    public PageResult likeQuery(CareerLikeQueryArgumentsDto careerLikeQueryArgumentsDto) {

        PageHelper.startPage(careerLikeQueryArgumentsDto.getCurrentPage(),careerLikeQueryArgumentsDto.getSize());

        Page<CareerListDetails> careerListDetails = careerMapper.likeQuery(careerLikeQueryArgumentsDto);

        return new PageResult(careerListDetails.getTotal(),careerListDetails.getResult());
    }

    /**
     * @Author: CK
     * @Date: 2023/12/22
     * @Param: [saveCareerDetailsDto]
     * @return: 成功结果集
     * @Description: 添加岗位
     */
    @Override
    @Transactional
    @ClearAllCache
    public Boolean saveCareer(SaveAndUpdateCareerDetailsDto saveCareerDetailsDto) {
        if (saveCareerDetailsDto.getJob_image() == null) {
            saveCareerDetailsDto.setJob_image(locations[new Random().nextInt(9)]);
        }

        Job job = new Job();
        BeanUtils.copyProperties(saveCareerDetailsDto,job);
        jobMapper.insertCareer(job);
        Integer id = job.getId();
        log.info("获取到的岗位id: {}" ,id);

        Company company = companyMapper.selectById(saveCareerDetailsDto.getCompanyId());

        Job_join jobJoin = new Job_join();
        BeanUtils.copyProperties(company,jobJoin);
        jobJoin.setJob_id(id);
        jobJoinMapper.insertJoin(jobJoin);
        return true;
    }

    /**
     * @Author: CK
     * @Date: 2023/12/27
     * @Param: [id, careerDetailsDto]
     * @return: 修改岗位信息标志
     * @Description: 根据ID修改岗位信息
     */

    @Override
    @Transactional
    @ClearAllCache
    public Boolean updateCareerInfoById(Integer id, SaveAndUpdateCareerDetailsDto careerDetailsDto) {

        LambdaUpdateWrapper<Job> updateWrapper = new LambdaUpdateWrapper<>();
        // 修改job表信息
        updateWrapper
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_name()),Job::getJob_name,careerDetailsDto.getJob_name())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_price()),Job::getJob_price,careerDetailsDto.getJob_price())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_address()),Job::getJob_address,careerDetailsDto.getJob_address())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_experience()),Job::getJob_experience,careerDetailsDto.getJob_experience())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_education()),Job::getJob_education,careerDetailsDto.getJob_education())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_welfare()),Job::getJob_welfare,careerDetailsDto.getJob_welfare())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_type()),Job::getJob_type,careerDetailsDto.getJob_type())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_description()),Job::getJob_description,careerDetailsDto.getJob_description())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_responsibility()),Job::getJob_responsibility,careerDetailsDto.getJob_responsibility())
                .set(Strings.isNotEmpty(careerDetailsDto.getJob_status()),Job::getJob_status,careerDetailsDto.getJob_status())
                .eq(Job::getId,id);

        jobService.update(updateWrapper);

        // 查询得到工作所属的信息

        Company company = companyMapper.selectById(careerDetailsDto.getCompanyId());
        Job_join jobJoin = new Job_join();

        BeanUtils.copyProperties(company,jobJoin);

        LambdaUpdateWrapper<Job_join> jjUpdateWrapper = new LambdaUpdateWrapper<>();
        jjUpdateWrapper.eq(Job_join::getJob_id,id);

        jobJoinMapper.update(jobJoin,jjUpdateWrapper);

        return true;
    }

    /**
     * @Author: CK
     * @Date: 2023/12/28
     * @Param: []
     * @return: 删除成功与否的标记
     * @Description: 根据Ids删除岗位
     */

    @Override
    @Transactional
    @ClearAllCache
    public Boolean removeCareerByIds(List<Integer> ids) {
        return jobService.removeByIds(ids) && jobJoinMapper.removeCareerByIds(ids);
    }

    /**
     * @Author: CK
     * @Date: 2023/12/29
     * @Param: [id]
     * @return:
     * @Description: 根据id获取职位信息
     */

    @Override
    public CareerDetailsVo getCareerInfoById(Integer id) {

        CareerDetailsVo careerDetailsVo = new CareerDetailsVo();

        Job job = jobMapper.selectById(id);

        if (Objects.isNull(job)) {
            throw new CareerIDIsNotFoundException("岗位id为空异常!");
        }

        BeanUtils.copyProperties(job,careerDetailsVo);

        LambdaQueryWrapper<Job_join> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Job_join::getJob_id,job.getId());

        String company = jobJoinMapper.selectOne(queryWrapper).getCompany();

        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyLambdaQueryWrapper.eq(Company::getCompany,company.trim());

        Company selectOne = companyMapper.selectOne(companyLambdaQueryWrapper);

        careerDetailsVo.setCompanyId(selectOne.getId());

        return careerDetailsVo;
    }

    /***
     * @Author: CK
     * @Date: 2024/3/12
     * @Param: [id]
     * @Description: 根据职位id获得信息
     */
    @Override
    public CareerInfoVo getFrontendCareerInfoById(Integer id) {

        if (Objects.isNull(id)) {
            throw new BaseException("职位编号为空 不允许操作！");
        }

        LambdaQueryWrapper<Job_join> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Job_join::getJob_id, id);
        Job_join jjInfo = jobJoinMapper.selectOne(queryWrapper);

        if (Objects.isNull(jjInfo)) {
            throw new BaseException("岗位信息已删除 请查看其他职位信息");
        }

        String company = jjInfo.getCompany();

        Job job = jobMapper.selectById(id);
        LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
        companyWrapper
                .eq(Company::getCompany, company);

        Company companyInfo = companyMapper.selectOne(companyWrapper);

        CareerInfoVo careerInfoVo = new CareerInfoVo();
        BeanUtils.copyProperties(job, careerInfoVo);

        careerInfoVo.setCompany(companyInfo);

        return careerInfoVo;
    }

    @Override
    public PageResult likeQueryByName(CareerLikeQueryArgumentsDto careerLikeQueryArgumentsDto) {
        PageHelper.startPage(careerLikeQueryArgumentsDto.getCurrentPage(),careerLikeQueryArgumentsDto.getSize());

        Page<CareerListDetails> careerListDetails = careerMapper.likeQueryByName(careerLikeQueryArgumentsDto);

        return new PageResult(careerListDetails.getTotal(),careerListDetails.getResult());
    }
}
